package com.situ.meetingRoom.service.impl;

import com.situ.meetingRoom.dao.BoardroomDao;
import com.situ.meetingRoom.dao.BookingDao;
import com.situ.meetingRoom.dao.impl.BoardroomDaoImpl;
import com.situ.meetingRoom.dao.impl.BookingDaoImpl;
import com.situ.meetingRoom.entity.Boardroom;
import com.situ.meetingRoom.entity.Booking;
import com.situ.meetingRoom.service.BoardroomService;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BoardroomVO;

import java.util.List;

public class BoardroomServiceImpl implements BoardroomService {
    private BoardroomDao boardroomDao = new BoardroomDaoImpl();

    @Override
    public List<BoardroomVO> getAll(Boardroom boardroom) {
        return boardroomDao.selectAll(boardroom);
    }

    @Override
    public int add(Boardroom boardroom) throws Exception {
//        保证那唯一的字段不能重复
        Boardroom boardroom1 = boardroomDao.selectByName(boardroom.getName());
        if (boardroom1 != null) {
            throw new Exception("会议室已存在");
        }
//        保存到数据库
        return boardroomDao.insert(boardroom);
    }

    @Override
    public int remove(Integer id) throws Exception {
        Boardroom boardroom = boardroomDao.selectById(id);
        //员工不存在
        if (boardroom == null) {
            throw new Exception("会议室不存在");
        }

        BookingDao bd = new BookingDaoImpl();
        Booking booking = bd.selectBoardroomId(boardroom.getId());

        if (booking != null && booking.getStatus() == 0){
            throw new Exception("有预约记录，无法删除");
        }

        //员工状态
        if (boardroom.getStatus() == 1) {
            throw new Exception("会议室以裁撤");
        }

        //设置员工状态
        boardroom.setStatus(1);

        //更新到数据库
        return boardroomDao.update(boardroom);
    }

    @Override
    public Boardroom getById(Integer id) {
        return boardroomDao.selectById(id);
    }

    @Override
    public int edit(Boardroom boardroom) throws Exception {
        Boardroom boardroom1 = boardroomDao.selectByName(boardroom.getName());
        //有效性 电话不能冲突

        if (boardroom1 != null && !boardroom1.getId().equals(boardroom.getId())) {
            throw new Exception("电话已被使用");
        }
        //部门ID验证
        return boardroomDao.update(boardroom);
    }

    @Override
    public PageInfo getForPage(Integer page, Integer limit, Boardroom boardroom) {
        return boardroomDao.selectForPage(page,limit ,boardroom);
    }
}
